package com.example.music.db;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

public class RecentPlayDAO {
    private DatabaseHelper dbHelper;
    private static final int MAX_RECENT_ITEMS = 50; // 最大最近播放数量

    public RecentPlayDAO(Context context) {
        dbHelper = new DatabaseHelper(context);
    }

    // 添加最近播放项
    public void addRecentItem(String itemId, int type) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();

        // 先检查是否已存在
        Cursor cursor = db.query(DatabaseHelper.TABLE_RECENT_PLAY,
                new String[]{DatabaseHelper.COLUMN_ID},
                DatabaseHelper.COLUMN_ITEM_ID + "=? AND " + DatabaseHelper.COLUMN_TYPE + "=?",
                new String[]{itemId, String.valueOf(type)},
                null, null, null);

        if (cursor != null && cursor.getCount() > 0) {
            // 更新播放时间
            ContentValues values = new ContentValues();
            values.put(DatabaseHelper.COLUMN_PLAY_TIME, System.currentTimeMillis());
            db.update(DatabaseHelper.TABLE_RECENT_PLAY, values,
                    DatabaseHelper.COLUMN_ITEM_ID + "=? AND " + DatabaseHelper.COLUMN_TYPE + "=?",
                    new String[]{itemId, String.valueOf(type)});
            cursor.close();
        } else {
            // 插入新记录
            ContentValues values = new ContentValues();
            values.put(DatabaseHelper.COLUMN_ITEM_ID, itemId);
            values.put(DatabaseHelper.COLUMN_TYPE, type);
            values.put(DatabaseHelper.COLUMN_PLAY_TIME, System.currentTimeMillis());
            db.insert(DatabaseHelper.TABLE_RECENT_PLAY, null, values);
        }

        // 检查并删除最旧的记录（先进先出）
        Cursor allCursor = db.query(DatabaseHelper.TABLE_RECENT_PLAY,
                null, null, null, null, null,
                DatabaseHelper.COLUMN_PLAY_TIME + " ASC");

        if (allCursor != null && allCursor.getCount() > MAX_RECENT_ITEMS) {
            allCursor.moveToPosition(0); // 移动到最旧的记录
            @SuppressLint("Range") int id = allCursor.getInt(allCursor.getColumnIndex(DatabaseHelper.COLUMN_ID));
            db.delete(DatabaseHelper.TABLE_RECENT_PLAY,
                    DatabaseHelper.COLUMN_ID + "=?",
                    new String[]{String.valueOf(id)});
        }

        if (allCursor != null) allCursor.close();
        db.close();
    }

    // 获取所有最近播放项
    @SuppressLint("Range")
    public List<RecentItem> getAllRecentItems() {
        List<RecentItem> items = new ArrayList<>();
        SQLiteDatabase db = dbHelper.getReadableDatabase();

        Cursor cursor = db.query(DatabaseHelper.TABLE_RECENT_PLAY,
                null, null, null, null, null,
                DatabaseHelper.COLUMN_PLAY_TIME + " DESC");

        if (cursor != null) {
            while (cursor.moveToNext()) {
                RecentItem item = new RecentItem();
                item.setId(cursor.getInt(cursor.getColumnIndex(DatabaseHelper.COLUMN_ID)));
                item.setItemId(cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_ITEM_ID)));
                item.setType(cursor.getInt(cursor.getColumnIndex(DatabaseHelper.COLUMN_TYPE)));
                item.setPlayTime(cursor.getLong(cursor.getColumnIndex(DatabaseHelper.COLUMN_PLAY_TIME)));
                items.add(item);
            }
            cursor.close();
        }
        db.close();
        return items;
    }

    // 清空最近播放
    public void clearRecentItems() {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        db.delete(DatabaseHelper.TABLE_RECENT_PLAY, null, null);
        db.close();
    }

    public static class RecentItem {
        private int id;
        private String itemId;
        private int type; // 0:歌曲, 1:歌单, 2:专辑
        private long playTime;

        // Getters and setters
        // ...

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getItemId() {
            return itemId;
        }

        public void setItemId(String itemId) {
            this.itemId = itemId;
        }

        public int getType() {
            return type;
        }

        public void setType(int type) {
            this.type = type;
        }

        public long getPlayTime() {
            return playTime;
        }

        public void setPlayTime(long playTime) {
            this.playTime = playTime;
        }
    }
}